package com.multithreading.chart1.join;

/**
 * @author chenqb
 * @project_name: java-multithreading
 * @package_name: com.cqb.join
 * @ClassName: joinDemo
 * @description: join等待其他线程先执行完在继续执行自己的任务。
 * Author by cqb, Date on 2020/6/27，23:14
 * PS: Not easy to write code, please indicate.
 */
public class JoinDemo {
    public static void main(String[] args) throws  InterruptedException {
        //创建线程threadOne
        Thread threadOne=new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println("child ThreadOne over!");
            }
        });
        //创建线程threadTwo
        Thread threadTwo=new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println("child ThreadTwo over!");
            }
        });
        //启动子线程
        threadOne.start();
        threadTwo.start();
        System.out.println("wait all child thread over!");

        //等待子线程执行完毕，返回
        threadTwo.join();
        threadOne.join();

        System.out.println("all child thread over!");
    }
}
